/**
 * 
 */

function validateForm() {

	var error = document.getElementById("error");
	while (error.hasChildNodes()) {
		error.removeChild(error.childNodes[0]);
	}
	var username = document.getElementById("username").value;
	var wardName = document.getElementById("wardName").value;
	var email = document.getElementById("email").value;
	var phoneNumber = document.getElementById("phoneNumber").value;
	var check = true;
	var textError = "";
	if (username == "" || username == null) {
		textError = "Username must be filled out";
		addElement(error, textError);
		check = false;
	}
	if (wardName == "" || wardName == null) {
		textError = "Ward name must be filled out";
		addElement(error, textError);
		check = false;
	}
	if (email == "" || email == null) {
		textError = "Email must be filled out";
		addElement(error, textError);
		check = false;
	}
	if (phoneNumber == "" || phoneNumber == null) {
		textError = "Phone number must be filled out";
		addElement(error, textError);
		check = false;
	}
	return check;
}

function addElement(error, textError) {
	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);
	var text = document.createTextNode(textError);
	dv.appendChild(text);
	error.appendChild(dv);
}

function getDistrict() {
	var city = $('#cityId').val();
	if (city == 0) {
		$('#districtId option[value!="0"]').remove();
	} else {
		$('#districtId option[value!="0"]').remove();

		$.ajax({
			"type" : "GET",
			"async" : false,
			"contentType" : "application/json;charset=utf-8",
			"dataType" : "json",
			"url" : "/web-client/client/getdistrictbycityid",
			"data" : {
				"cityId" : $('#cityId').val(),
			},
			"success" : function(data) {
				$.each(data, function(index, value) {
					$('#districtId').append(
							$('<option>').text(value.districtName).attr(
									'value', value.districtId));
				});
			},
			"error" : function(data) {
				alert("ERROR WHEN GET DISTRICT!");
				flag = false;
			}
		});
	}
}

function checkArea() {
	var flag = true;
	var city = $('#cityId').val();
	if (city == 0) {
		alert("Please choose city!");
		return false;
	}
	var district = $('#districtId').val();
	if (district == 0) {
		alert("Please choose district!");
		return false;
	}
	return flag;
}

function chooseFoods(restaurantId, foodId, foodName, price, deliveryPrice,
		minOrder) {
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/web-client/client/addfood",
		"data" : {
			"restaurantId" : restaurantId,
			"foodId" : foodId,
			"foodName" : foodName,
			"price" : price
		},
		"success" : function(data) {
			location.reload();
		},
		"error" : function(data) {
			alert("error add food!");
		}
	});
}

function chooseDrinks(restaurantId, drinkId, drinkName, price, deliveryPrice,
		minOrder) {
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/web-client/client/adddrink",
		"data" : {
			"restaurantId" : restaurantId,
			"drinkId" : drinkId,
			"drinkName" : drinkName,
			"price" : price
		},
		"success" : function(data) {
			location.reload();
		},
		"error" : function(data) {
			alert("error add drink!");
		}
	});
}

function removeFood(restaurantId, foodId) {
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/web-client/client/removefoodbasket",
		"data" : {
			"restaurantId" : restaurantId,
			"foodId" : foodId
		},
		"success" : function(data) {
			location.reload();
		},
		"error" : function(data) {
			alert("error remove food basket!");
		}
	});
}

function removeDrink(restaurantId, drinkId) {
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/web-client/client/removedrinkbasket",
		"data" : {
			"restaurantId" : restaurantId,
			"drinkId" : drinkId
		},
		"success" : function(data) {
			location.reload();
		},
		"error" : function(data) {
			alert("error remove drink basket!");
		}
	});
}

function showAndHidden() {
	var count = document.getElementById("count").value;
	var minOrder = document.getElementById("minOrder").value;
	var deliveryPrice = document.getElementById("deliveryPrice").value;
	count = new Number(count);
	minOrder = new Number(minOrder);
	deliveryPrice = new Number(deliveryPrice);
	if (count.toString() == deliveryPrice.toString()) {
		$("#order_yes").hide();
	} else if (deliveryPrice < count && count < minOrder) {
		$("#btn_order").hide();
		$("#order_no").hide();
	} else {
		$("#order_no").hide();
	}
}